package jim.org;

import java.awt.Color;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;
import util.Attributes;

/**
 * @author Jonas Dietz and Ramon Victor from the Cologne University of Applied Sciences
 * @since 2012
 * This class handles the Options.
 */
@SuppressWarnings("serial")
public class Preferences extends JFrame {

    private JPanel contentPane;
    private JLabel lblHereYouCan;
    private JLabel lblTextcolor;
    private JLabel lblBackgroundcolor;
    private Panel textColorPanel;
    private Panel bgColorPanel;
    private JButton btnOkay;
    private JButton btnAbbrechen;
    private Color tmpTextColor;
    private Color tmpBgColor;
    private JButton btnDefault;
    private JButton btnRandomize;
    private Random r = new Random();

    /**
     * Create the frame.
     */
    public Preferences() {
	setTitle(Attributes.rs.getString("settings"));
	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	setBounds(100, 100, 520, 148);
	contentPane = new JPanel();
	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	setContentPane(contentPane);
	contentPane.setLayout(new MigLayout("", "[][][][][][][][][][][]", "[][][][][][][][][][]"));

	lblHereYouCan = new JLabel(Attributes.rs.getString("setYourPreferences"));
	contentPane.add(lblHereYouCan, "cell 0 0,alignx left,aligny top");

	lblTextcolor = new JLabel(Attributes.rs.getString("textColor"));
	contentPane.add(lblTextcolor, "cell 0 2,alignx left,aligny top");

	textColorPanel = new Panel();
	textColorPanel.addMouseListener(new TextColorPanelMouseListener());
	textColorPanel.setBackground(Attributes.textColor);
	contentPane.add(textColorPanel, "cell 1 2");

	lblBackgroundcolor = new JLabel(Attributes.rs.getString("backgroundColor"));
	contentPane.add(lblBackgroundcolor, "cell 0 3,alignx left,aligny top");

	bgColorPanel = new Panel();
	bgColorPanel.addMouseListener(new BgColorPanelMouseListener());
	bgColorPanel.setBackground(Attributes.backgroundColor);
	contentPane.add(bgColorPanel, "cell 1 3");

	btnOkay = new JButton(Attributes.rs.getString("okay"));
	btnOkay.addActionListener(new BtnOkayActionListener());

	btnRandomize = new JButton(Attributes.rs.getString("randomize"));
	btnRandomize.addActionListener(new BtnRandomizeActionListener());
	contentPane.add(btnRandomize, "cell 0 4,alignx left,aligny bottom");
	contentPane.add(btnOkay, "cell 8 4,alignx right,aligny bottom");

	btnAbbrechen = new JButton(Attributes.rs.getString("cancel"));
	btnAbbrechen.addActionListener(new BtnAbbrechenActionListener());

	btnDefault = new JButton(Attributes.rs.getString("default"));
	btnDefault.addMouseListener(new BtnDefaultMouseListener());
	contentPane.add(btnDefault, "cell 9 4,alignx right,aligny bottom");
	contentPane.add(btnAbbrechen, "cell 10 4,alignx right,aligny bottom");
    }

    /**
     * Launch the application.
     */
    public static void run() {
	try {
	    Preferences frame = new Preferences();
	    frame.setVisible(true);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    /**
     * Activates the JColorChooser for the Background Color.
     */
    private class BgColorPanelMouseListener extends MouseAdapter {
	@Override
	public void mouseClicked(MouseEvent e) {
	    tmpBgColor = JColorChooser.showDialog(null, Attributes.rs.getString("chooseBackground"), bgColorPanel.getBackground());
	    bgColorPanel.setBackground(tmpBgColor);
	}
    }

    /**
     * Standard cancel operation.
     */
    private class BtnAbbrechenActionListener implements ActionListener {
	@Override
	public void actionPerformed(ActionEvent arg0) {
	    dispose();
	    setVisible(false);
	    setEnabled(false);
	}
    }

    /**
     * Sets the Colortheme to the default jim theme.
     */
    private class BtnDefaultMouseListener extends MouseAdapter {
	@Override
	public void mouseClicked(MouseEvent e) {
	    textColorPanel.setBackground(new Color(255, 112, 1));
	    bgColorPanel.setBackground(new Color(46, 54, 52));
	}
    }

    /**
     * Close the frame and saves the options to a file.
     */
    private class BtnOkayActionListener implements ActionListener {
	@Override
	public void actionPerformed(ActionEvent arg0) {
	    JOptionPane.showMessageDialog(null, Attributes.rs.getString("restartJIM"));
	    Attributes.saveAttributes(textColorPanel.getBackground(), bgColorPanel.getBackground());
	    dispose();
	    setVisible(false);
	    setEnabled(false);
	}
    }

    /**
     * Awesome random color generator!
     */
    private class BtnRandomizeActionListener implements ActionListener {
	@Override
	public void actionPerformed(ActionEvent arg0) {
	    Color rndTextColor = new Color(0, 0, 0), rndBgColor = new Color(0, 0, 0);

	    int[] rnd = new int[3];
	    for(int i = 0; i < 2; i++){
		for(int j = 0; j < 3; j++){
		    rnd[j] = (int) ((r.nextInt(256) * System.currentTimeMillis()) % 256);
		}
		if(i == 0) rndTextColor = new Color(rnd[0], rnd[1], rnd[2]);
		else rndBgColor = new Color(rnd[0], rnd[1], rnd[2]);
	    }
	    textColorPanel.setBackground(rndTextColor);
	    bgColorPanel.setBackground(rndBgColor);
	}
    }

    /**
     * Activates the JColorChooser for the text color.
     */
    private class TextColorPanelMouseListener extends MouseAdapter {
	@Override
	public void mouseClicked(MouseEvent arg0) {
	    tmpTextColor = JColorChooser.showDialog(null, Attributes.rs.getString("chooseText"), textColorPanel.getBackground());
	    textColorPanel.setBackground(tmpTextColor);
	}
    }
}
